/*
 * OpenButton.java
 * 
 * Copyright 2007 Tiberiumlabs
 *
 * This file is part of Tiberiumlabs Shards.
 *
 * Tiberiumlabs Shards is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Tiberiumlabs Shards is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.tiberiumlabs.shards.components;

import java.awt.Insets;
import java.lang.ref.SoftReference;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;

/**
 * Small button with folder icon, containing empty text and can be easyly used by GUI Builders.
 * Optimized for 17x17 icon.
 *
 * @author <a href="mailto:paranoid.tiberiumlabs@gmail.com">Paranoid</a>
 */
public class OpenButton extends JButton {
    
    private static final String TEXT = " ";
    private static final Insets INSETS = new Insets(0, 3, 0, 0);
    private static SoftReference<Icon> softIcon = null;
    
    /**
     * Create small button with default 17x17 icon.
     */
    public OpenButton() {
        super(getDefaultIcon());
        init();
    }
    
    /**
     * Create small button with default 17x17 icon and specifyed action.
     * @param action the <code>Action</code> used to specify the new button
     */
    public OpenButton(Action action) {
        super(action);
        setIcon(getDefaultIcon());
        init();
    }
    
    /**
     * Create small button with specifyed icon and action.
     * @param action the <code>Action</code> used to specify the new button
     * @param icon the Icon image to display on the button
     */
    public OpenButton(Action action, Icon icon) {
        super(action);
        setIcon(icon);
        init();
    }
    
    /**
     * Create small button with specifyed icon.
     * @param icon the Icon image to display on the button
     */
    public OpenButton(Icon icon) {
        super(icon);
        init();
    }
    
    private void init() {
        setText(TEXT);
        setIconTextGap(0);
        setMargin(INSETS);
        setMaximumSize(getPreferredSize());
    }
    
    /**
     * Sets the button's default icon. This icon is
     * also used as the "pressed" and "disabled" icon if
     * there is no explicitly set pressed icon.
     *
     * @param icon the icon used as the default image
     */
    public void setImage(Icon icon) {
        setIcon(icon);
    }
    
    private static final Icon getDefaultIcon() {
        Icon icon;
        if (softIcon != null) {
            icon = softIcon.get();
            if (icon != null) {
                return icon;
            }
        }
        icon = new ImageIcon(OpenButton.class.getResource("folder_small.png"));
        softIcon = new SoftReference<Icon>(icon);
        return icon;
    }

}
